package com.arges.web.common;

import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * excel表格处理类
 * 
 * @author arges
 *
 */
public class JXLHandler
{
	/**
	 * 设置excel表格样式
	 * 
	 * @param writableSheet
	 * @param title
	 * @param heads
	 * @return
	 */
	public static WritableSheet setSheetStyle(WritableSheet writableSheet, String title, String[] heads)
	{

		try
		{
			/**
			 * 定义单元格样式
			 */
			WritableFont wf_title = new WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD, false,
					UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
			WritableFont wf_head = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false,
					UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色

			WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 单元格定义
			wcf_title.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
			wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
			wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE);

			WritableCellFormat wcf_head = new WritableCellFormat(wf_head);
			wcf_head.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
			wcf_head.setAlignment(jxl.format.Alignment.CENTRE);
			wcf_head.setVerticalAlignment(VerticalAlignment.CENTRE);

			if (null != title)
			{
				writableSheet.setRowView(0, 1000); // 设置行的高度
				writableSheet.addCell(new Label(0, 0, title, wcf_title)); // 普通的带有定义格式的单元格
				writableSheet.mergeCells(0, 0, heads.length - 1, 0); // 合并单元格
				writableSheet.setRowView(1, 500); // 设置行的高度
			}
			else
			{
				writableSheet.setRowView(0, 500); // 设置行的高度
			}
			for (int i = 0; i < heads.length; i++)
			{
				writableSheet.setColumnView(i, 15); // 设置列的宽度
				if (null != title)
				{
					writableSheet.addCell(new Label(i, 1, heads[i], wcf_head));
				}
				else
				{
					writableSheet.addCell(new Label(i, 0, heads[i], wcf_head));
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return writableSheet;
	}

	/**
	 * 表格内容样式
	 * 
	 * @return
	 */
	public static WritableCellFormat getTableStyle()
	{
		WritableCellFormat wcf_table = null;
		try
		{
			WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
					UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
			wcf_table = new WritableCellFormat(wf_table);
			wcf_table.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
			wcf_table.setAlignment(jxl.format.Alignment.CENTRE);
			wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return wcf_table;
	}

	/**
	 * 错误内容样式
	 * 
	 * @return
	 */
	public static WritableCellFormat getErrorStyle()
	{
		WritableCellFormat wcf_table = null;
		try
		{
			WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
					UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED); // 定义格式 字体 下划线 斜体 粗体 颜色
			wcf_table = new WritableCellFormat(wf_table);
			wcf_table.setAlignment(jxl.format.Alignment.CENTRE);
			wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return wcf_table;
	}

	/**
	 * 关闭工作流
	 * 
	 * @param workbook
	 * @param writableWorkbook
	 */
	public static void close(Workbook workbook, WritableWorkbook writableWorkbook)
	{
		try
		{
			if (null != workbook)
			{
				workbook.close();
			}
			if (null != writableWorkbook)
			{
				writableWorkbook.close();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
